﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Home.Haushalt.Lib;
using Home.Haushalt.Lib.Element;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmPropertyMember : FrmProperty
    {
        /// <summary>
        /// 
        /// </summary>
        public FrmPropertyMember()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmPropertyMember_Load(object sender, EventArgs e)
        {
            AddBinding(txtName.DataBindings, "Text", Member.Fields.Name);
            AddBinding(txtUser.DataBindings, "Text", Member.Fields.UserName);
            AddBinding(txtPass.DataBindings, "Text", Member.Fields.Pass);
            AddBinding(txtLink.DataBindings, "Text", Member.Fields.Link);

            lstOpponent.AddBinding(_elementObject, Member.Fields.OpponentId);
            lstPosten.AddBinding(_elementObject, Member.Fields.PostenId);
            lstDocument.AddBinding(_elementObject, Member.Fields.DocumentId);
            lstMembertype.AddBinding(_elementObject, Member.Fields.Art);

            DataBind();

            frmAL_OnAction(this, new HEventArgs(""));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbShowPass_CheckedChanged(object sender, EventArgs e)
        {
            txtPass.PasswordChar = (cbShowPass.Checked) ? (char)0 : '*';
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAutoLogin_Click(object sender, EventArgs e)
        {
            FrmAutoLogin frmAL = new FrmAutoLogin(_elementObject);
            frmAL.OnAction += new EventHandler<HEventArgs>(frmAL_OnAction);
            frmAL.Show();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmAL_OnAction(object sender, HEventArgs e)
        {
            bool autoLoginExists = DAO.HFile.AutoLoginExists(_elementObject.ID);
            btnAutoLoginTest.Enabled = autoLoginExists;
            btnAutoLoginDelete.Enabled = autoLoginExists;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAutoLoginTest_Click(object sender, EventArgs e)
        {
            FrmAutoLogin.StartAutoLogin(_elementObject.ID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAutoLoginDelete_Click(object sender, EventArgs e)
        {
            if (!DAO.HFile.DeleteAutoLoginFile(_elementObject.ID))
            {
                Journal.Add(this, ActionResult.Nok, "Log-Datei konnte nicht gelöscht werden");
            }

            frmAL_OnAction(null, null);
        }

    }
}
